package com.leagsoft.emm.demo;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.huawei.secoclient.sslvpn.ConstValue;
import com.huawei.secoclient.sslvpn.VpnUtils;
import com.huawei.secoclient.sslvpn.entity.LoginEntity;
import com.leagsoft.emm.demo.ui.ClearEditText;
import com.leagsoft.emm.demo.util.ToastUtil;

/**
 * 账号+密码登陆界面
 */
public class LoginActivity extends AppCompatActivity {
    public static final String TAG = "LoginActivity";
    private ClearEditText etUserName;
    private ClearEditText etPassword;
    private Button btLogin;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        etUserName = findViewById(R.id.et_user_name);
        etPassword = findViewById(R.id.et_password);
        btLogin = findViewById(R.id.bt_login);

        btLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = etUserName.getText().toString();
                String password = etPassword.getText().toString();
                LoginEntity loginEntity = new LoginEntity(0);
                loginEntity.setUserName(username);
                loginEntity.setPassword(password);
//                loginEntity.setDeviceCACheck(0);//是否支持本地证书验证
//                loginEntity.setCACheckCertName("");//本地证书路径地址
                boolean isLogin = VpnUtils.startLogin(loginEntity, code -> {
                    Log.i(TAG, "login callback:" + code);
                    if (code == ConstValue.LOGIN_SUCCESS) {
                        ToastUtil.show(LoginActivity.this, "登录成功 响应码:" + code);
                        finish();
                    } else if (code == ConstValue.LOGIN_VERIFY_SERVER) {
                        ToastUtil.show(LoginActivity.this, "本地服务器验证失败 响应码:" + code);
                    } else if (code == ConstValue.LOGIN_NEED_MODIFY_PASSWORD) {
                        startActivity(new Intent(LoginActivity.this, ModifyPasswordActivity.class));
                        ToastUtil.show(LoginActivity.this, "需要修改密码 响应码:" + code);
                        finish();
                    } else if (code == ConstValue.LOGIN_NEED_SEND_VERIFICATION_CODE) {
                        startActivity(new Intent(LoginActivity.this, SimWarningCheckActivity.class));
                        ToastUtil.show(LoginActivity.this, "需要进行双因子验证 响应码:" + code);
                        finish();
                    } else {
                        ToastUtil.show(LoginActivity.this, "登录失败 响应码:" + code);
                    }
                });
                if (isLogin) {
                    ToastUtil.show(LoginActivity.this, "登录构建成功");
                } else {
                    ToastUtil.show(LoginActivity.this, "登录构建失败");
                }
            }
        });
    }
}
